/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.internal.platform;

import java.lang.reflect.Field;

import org.sourceprojects.lycia.internal.platform.android.AndroidApiVersion;

public class PlatformSupportUtils {

	// Cache value, no need for being threadsafe
	private static AndroidApiVersion androidApiVersion = null;
	private static Boolean dalvik = null;
	private static Boolean log4j = null;
	private static Boolean bytecodeGeneration = null;

	private PlatformSupportUtils() {
	}

	public static final boolean isDalvikDependingPlatform() {
		if (dalvik != null) {
			return dalvik;
		}

		final ClassLoader cl = PlatformSupportUtils.class.getClassLoader();
		final String canonicalName = cl.getClass().getCanonicalName();

		dalvik = canonicalName.contains("dalvik");

		return dalvik;
	}

	public static final boolean isLog4jAvailable() {
		if (log4j != null) {
			return log4j;
		}

		try {
			Class.forName("org.apache.log4j.Logger");
			log4j = true;
		} catch (final ClassNotFoundException e) {
			log4j = false;
		}

		return log4j;
	}

	public static final boolean isBytecodeGenerationAvailable() {
		if (bytecodeGeneration != null) {
			return bytecodeGeneration;
		}

		try {
			Class.forName("org.objectweb.asm.ClassReader");
			bytecodeGeneration = true;
		} catch (final ClassNotFoundException e) {
			bytecodeGeneration = false;
		}

		return bytecodeGeneration;
	}

	public static final AndroidApiVersion getAndroidApiVersion() {
		if (!isDalvikDependingPlatform()) {
			return AndroidApiVersion.IllegalPlatform;
		}

		if (androidApiVersion != null) {
			return androidApiVersion;
		}

		try {
			final Class<?> clazz = Class.forName("android.os.Build$VERSION");
			final Object version = clazz.newInstance();

			final Field sdkVersion = clazz.getField("SDK_INT");
			sdkVersion.setAccessible(true);

			final int level = sdkVersion.getInt(version);
			androidApiVersion = AndroidApiVersion.getByApiLevel(level);

			return androidApiVersion;

		} catch (final ClassNotFoundException e) {
			return AndroidApiVersion.IllegalPlatform;
		} catch (final InstantiationException e) {
			return AndroidApiVersion.IllegalPlatform;
		} catch (final IllegalAccessException e) {
			return AndroidApiVersion.IllegalPlatform;
		} catch (final SecurityException e) {
			return AndroidApiVersion.IllegalPlatform;
		} catch (final NoSuchFieldException e) {
			return AndroidApiVersion.IllegalPlatform;
		}
	}

}
